﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO
Imports System.Net
Imports System.Text

Imports com.google.api

Imports Yahoo.API

Imports eBay.Service.Call
Imports eBay.Service.Core.Soap
Imports eBay.Service.Core.Sdk
Imports eBay.Service.Util

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim apiContext As New ApiContext

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click
        If Page.IsValid Then
            Dim _Points As New BusinessLogic.Points
            _Points.Activity = 0
            _Points.IPAddress = Request.UserHostAddress
            _Points.Comments = txtKeyword.Text
            If Session("Username") IsNot Nothing Then
                _Points.Username = Session("Username")
            Else
                _Points.Username = ""
            End If
            _Points.Save()

            Session("YahooPage") = 1
            newsRes.PageIndex = 0
            newsResult.Visible = True
            GetNewsSearchWebServiceResults()
            imgBlank.Height = 30
            imgFrame.Visible = False
            pnlHyperlinkBottom.Visible = True
            pnlImage1.Visible = False
            pnlImage2.Visible = True
        End If
    End Sub
  

   
    Private Sub GetNewsSearchWebServiceResults()
        Dim srch As String = ""
        srch = txtKeyword.Text

        Dim yahoo As New YahooSearchService
        Dim _PageCount = 8
        Dim _SearchResults As New ArrayList()
        Dim Count As Integer = _PageCount
        Dim Offset As Integer = (Integer.Parse(Session("YahooPage").ToString()) - 1) * _PageCount + 1

        Dim resultSet As Yahoo.API.NewsSearchResponse.ResultSet
        Dim YahooApiKey As String = ConfigurationManager.AppSettings("YahooAPIKey").ToString
        resultSet = yahoo.NewsSearch(YahooApiKey, srch, "all", Count, Offset, "rank", "en")
        Try
            If resultSet.Result.Length > 0 Then
                For Each result As Yahoo.API.NewsSearchResponse.ResultType In resultSet.Result
                    Dim _TempSearchResult As New SearchResult()
                    _TempSearchResult.Url = result.Url
                    _TempSearchResult.Title = result.Title
                    _TempSearchResult.Summary = result.Summary
                    _TempSearchResult.PublishDate = result.PublishDate
                    If result.Thumbnail IsNot Nothing Then
                        _TempSearchResult.Thumbnail = result.Thumbnail.Url
                    Else
                        _TempSearchResult.Thumbnail = ""
                    End If
                    _SearchResults.Add(_TempSearchResult)
                Next
            End If
        Catch ex As Exception
        End Try
        litYahooSearchCount.Text = String.Format("Page {0} of {1}", Session("YahooPage"), CInt(resultSet.totalResultsAvailable / _PageCount))
        If Integer.Parse(Session("YahooPage").ToString()) = 1 Then
            lnkYahooPrev.Visible = False
        Else
            lnkYahooPrev.Visible = True
        End If
        If Integer.Parse(Session("YahooPage").ToString()) = CInt(resultSet.totalResultsAvailable / _PageCount) Then
            lnkYahooNext.Visible = False
        Else
            lnkYahooNext.Visible = True
        End If
        newsRes.DataSource = _SearchResults
        newsRes.DataBind()

    End Sub
    Protected Function CreateNewsImage(ByVal _Image As String, ByVal _Summary As String) As String
        Dim _RetVal As String = ""
        If _Image.Length > 0 Then
            _RetVal = [String].Format("<img src=""{0}"" title=""{1}"" alt=""Summary""/>", _Image, _Summary)
        End If
        Return _RetVal
    End Function

    Protected Sub newsRes_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles newsRes.PageIndexChanged
        'Protected Sub newsRes_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles newsRes.PageIndexChanged
        'newsRes.PageIndex = e.NewPageIndex
        newsRes.DataSource = ViewState("resultNewsElmSet")
        newsRes.DataBind()
    End Sub

    Protected Sub lnkYahooPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooPrev.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) - 1
        GetNewsSearchWebServiceResults()
    End Sub
    Protected Sub lnkYahooNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooNext.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) + 1
        GetNewsSearchWebServiceResults()
    End Sub

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = True
        If txtKeyword.Text.Length = 0 Then
            args.IsValid = False
        End If
    End Sub

    Private Sub SetEbayCredential()
        apiContext.ApiCredential.ApiAccount.Developer = ""
        apiContext.ApiCredential.ApiAccount.Application = ""
        apiContext.ApiCredential.ApiAccount.Certificate = ""
        apiContext.ApiCredential.eBayAccount.UserName = ""
        apiContext.ApiCredential.eBayAccount.Password = ""
        apiContext.ApiCredential.eBayToken = ""
        apiContext.SoapApiServerUrl = ""
        apiContext.SignInUrl = ""
        apiContext.EPSServerUrl = ""
        apiContext.Version = ""
        apiContext.ApiLogManager = New ApiLogManager()
        apiContext.ApiLogManager.ApiLoggerList.Add(New FileLogger(Server.MapPath(ConfigurationManager.AppSettings.Get("LogFileName")), True, True, True))
        apiContext.ApiLogManager.EnableLogging = True
        apiContext.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings.Get("TimeOut").ToString)

        If Not ConfigurationManager.AppSettings.Get("EbayDevID") Is Nothing Then
            apiContext.ApiCredential.ApiAccount.Developer = ConfigurationManager.AppSettings("EbayDevID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayDevID") Is Nothing Then
            apiContext.ApiCredential.ApiAccount.Developer = ConfigurationManager.AppSettings("EbayDevID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayDevID") Is Nothing Then
            apiContext.ApiCredential.ApiAccount.Developer = ConfigurationManager.AppSettings("EbayDevID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayAppID") Is Nothing Then
            apiContext.ApiCredential.ApiAccount.Application = ConfigurationManager.AppSettings("EbayAppID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayCertID") Is Nothing Then
            apiContext.ApiCredential.ApiAccount.Certificate = ConfigurationManager.AppSettings("EbayCertID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayAuthToken") Is Nothing Then
            apiContext.ApiCredential.eBayToken = ConfigurationManager.AppSettings("EbayAuthToken").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayUserID") Is Nothing Then
            apiContext.ApiCredential.eBayAccount.UserName = ConfigurationManager.AppSettings("EbayUserID").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayUserPswd") Is Nothing Then
            apiContext.ApiCredential.eBayAccount.Password = ConfigurationManager.AppSettings("EbayUserPswd").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EbayApiServerUrl") Is Nothing Then
            apiContext.SoapApiServerUrl = ConfigurationManager.AppSettings("EbayApiServerUrl").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("SignInUrl") Is Nothing Then
            apiContext.SignInUrl = ConfigurationManager.AppSettings("SignInUrl").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("EpsServerUrl") Is Nothing Then
            apiContext.EPSServerUrl = ConfigurationManager.AppSettings("EpsServerUrl").ToString
        End If

        If Not ConfigurationManager.AppSettings.Get("Version") Is Nothing Then
            apiContext.Version = ConfigurationManager.AppSettings("Version").ToString
        End If
    End Sub
   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not String.IsNullOrEmpty(Request.QueryString("Q")) Then
                Session("YahooPage") = 1
                newsRes.PageIndex = 0
                newsResult.Visible = True
                txtKeyword.Text = Request.QueryString("Q")
                GetNewsSearchWebServiceResults()
                imgBlank.Height = 30
                imgFrame.Visible = False
                pnlHyperlinkBottom.Visible = True
                pnlImage1.Visible = False
                pnlImage2.Visible = True
            ElseIf Session("SearchText") IsNot Nothing Then
                txtKeyword.Text = Session("SearchText").ToString()
                Session("YahooPage") = 1
                newsRes.PageIndex = 0
                newsResult.Visible = True
                GetNewsSearchWebServiceResults()
                imgBlank.Height = 30
                imgFrame.Visible = False
                pnlHyperlinkBottom.Visible = True
                pnlImage1.Visible = False
                pnlImage2.Visible = True
            Else
                txtKeyword.Text = ConfigurationManager.AppSettings("RecentNewsTopStory")
                Session("YahooPage") = 1
                newsRes.PageIndex = 0
                newsResult.Visible = True
                GetNewsSearchWebServiceResults()
                imgBlank.Height = 30
                imgFrame.Visible = False
                pnlHyperlinkBottom.Visible = True
                pnlImage1.Visible = False
                pnlImage2.Visible = True
            End If
        Else
            If txtKeyword.Text.Length > 0 Then
                Session("SearchText") = txtKeyword.Text
            Else
                Session("SearchText") = Nothing
            End If
        End If
    End Sub

    Public Class SearchResult
        Private _Url As String
        Property Url() As String
            Get
                Return _Url
            End Get
            Set(ByVal value As String)
                _Url = value
            End Set
        End Property

        Private _Title As String
        Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Private _Summary As String
        Property Summary() As String
            Get
                Return _Summary
            End Get
            Set(ByVal value As String)
                _Summary = value
            End Set
        End Property


        Private _PublishDate As String
        Property PublishDate() As String
            Get
                Return _PublishDate
            End Get
            Set(ByVal value As String)
                _PublishDate = value
            End Set
        End Property

        Private _Thumbnail As String
        Property Thumbnail() As String
            Get
                Return _Thumbnail
            End Get
            Set(ByVal value As String)
                _Thumbnail = value
            End Set
        End Property
    End Class
End Class